<template>
	<div class="card flex_col m5 p10">
		<label class="flex_center_self">OTA软件更新</label>
		<div class="flex_row_center mt5 flex_center_self">
			<label>软件版本:{{ globalStatus.version }}</label>
			<el-button @click="onClickUpdate" class="ml5" type="primary">更新</el-button>
		</div>
	</div>
</template>

<script setup lang="ts">
import { dialog, fs } from '@tauri-apps/api';
import { globalStatus } from '../service/recv_notify';
import { runAction } from '../utils/ui';
import service from '../service/service';

async function onClickUpdate() {

	const result = await dialog.open({
		title: '固件',
		multiple: false,
		filters: [
			{
				name: '',
				extensions: ['bin'],
			}
		]
	});
	if (result == null) {
		return;
	}
	const path = result as string;
	const data = await fs.readBinaryFile(path);

	runAction('软件更新', '更新', async (dialog) => {
		await service.ota(data, (prog: number) => {
			dialog.setMsg(`进度:${prog}%`);
		});
	});
}



</script>
